package com.mack.batterypicture.slice;

import com.mack.batterypicture.ResourceTable;
import com.mack.batterypicture.devices.SelectDeviceDialog;
import com.mack.batterypicture.utils.Const;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.bundle.IBundleManager;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

import static ohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG = MainAbilitySlice.class.getSimpleName();

    private static final int PERMISSION_CODE = 10000000;

    private List<DeviceInfo> devices = new ArrayList<>();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        grantPermission();
        initView();
    }

    // 检查分布式权限
    void grantPermission() {
        if (verifySelfPermission(DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
            if (canRequestPermission(DISTRIBUTED_DATASYNC)) {
                requestPermissionsFromUser(new String[]{DISTRIBUTED_DATASYNC}, PERMISSION_CODE);
            }
        }
    }

    private void initView() {
        findComponentById(ResourceTable.Id_start).setClickedListener(new ButtonClick());
    }

    // 获取设备list
    private void getDevices() {
        if (devices.size() > 0) {
            devices.clear();
        }
        List<DeviceInfo> deviceInfos =
                DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        devices.addAll(deviceInfos);
        showDevicesDialog();
    }

    // 弹出框显示设备列表，点击
    private void showDevicesDialog() {
        new SelectDeviceDialog(this, devices, deviceInfo -> {
            startLocalFa(deviceInfo.getDeviceId());
            startRemoteFa(deviceInfo.getDeviceId());
        }).show();
    }

    // 开始本地FA
    private void startLocalFa(String deviceId) {
        Intent intent = new Intent();
        intent.setParam(Const.KEY_REMOTE_DEVICEID, deviceId);
        intent.setParam(Const.KEY_IS_LOCAL, true);
        Operation operation = new Intent.OperationBuilder().withBundleName(getBundleName())
                .withAbilityName(Const.ABILITY_MAIN)
                .withAction(Const.PACKAGE_PAGE)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    // 远程设备Fa
    private void startRemoteFa(String deviceId) {
        String localDeviceId =
                KvManagerFactory.getInstance().createKvManager(new KvManagerConfig(this)).getLocalDeviceInfo().getId();
        Intent intent = new Intent();
        intent.setParam(Const.KEY_REMOTE_DEVICEID, localDeviceId);
        intent.setParam(Const.KEY_IS_LOCAL, false);
        Operation operation = new Intent.OperationBuilder().withDeviceId(deviceId)
                .withBundleName(getBundleName())
                .withAbilityName(Const.ABILITY_MAIN)
                .withAction(Const.PACKAGE_PAGE)
                .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private class ButtonClick implements Component.ClickedListener {
        @Override
        public void onClick(Component component) {
            int btnId = component.getId();
            switch (btnId) {
                case ResourceTable.Id_start:
                    getDevices();
                    break;
                default:
                    break;
            }
        }
    }
}
